(function(window){
    function TabBar(pagePath){
        let tabBarAllWrap = document.createElement('section');
        tabBarAllWrap.classList.add('kh_dhl');
        document.querySelector('body').appendChild(tabBarAllWrap);
        const config = window.config;
        const pages = config.pages;
        createTabbar('.kh_dhl',pagePath);
        function createTabbar(wrap_name,page){
            let tabBarArr = config.tabBar
            let wrap = document.querySelector(wrap_name);
            for(let i = 0;i<tabBarArr.length;i++){
                let e = tabBarArr[i];
                let tabWrap = document.createElement('section');
                tabWrap.classList.add(`tab-item${i}`);
                tabWrap.innerHTML = `<img src="${e.iconPath}"><br>${e.text}`;
                wrap.appendChild(tabWrap);
            }
            let tabItemArr = wrap.querySelectorAll('section');
            function tabInit(){
                for(let i=0;i<tabItemArr.length;i++){
                    let e = tabItemArr[i];
                    e.style.color = '#000';
                    e.querySelector('img').src = tabBarArr[i].iconPath;
                }
            }
            for(let i=0;i<tabItemArr.length;i++){
                let item = tabItemArr[i];
                let p_item = tabBarArr[i];
                if(p_item.pagePath === page){
                    item.style.color = '#1798DC';
                    item.querySelector('img').src = tabBarArr[i].selectedIconPath;
                }
                item.addEventListener('click',(e)=>{
                    tabInit();
                    item.style.color = '#1798DC';
                    item.querySelector('img').src = tabBarArr[i].selectedIconPath;
                    window.location.href = pages[tabBarArr[i].pagePath]
                })
            }
        }
    }
    //获取浏览器地址内的信息
    function getParam(){
        let url = window.location.href;
        let paramStr = url.split('?')[1];
        let paramArr = paramStr.split('&');
        let paramObj = {};
        for (let i = 0; i < paramArr.length; i++) {
            let p_item = paramArr[i];
            paramObj[p_item.split('=')[0]] = p_item.split('=')[1]
        }
        return paramObj;
    }
    /**
     * 动态创建
     * @param {String} bigboxname 外层盒子
     * @param {String} classname 新盒子类名
     * @param {String} content 新盒子内容
     * @param {String} newboxname 新盒子类名
     */
    function CreatBox(bigboxname,classname,content,newboxname){
        if(!newboxname){
           newboxname = 'div'; 
        }
        let OutBoxname=document.getElementsByClassName(bigboxname)[0]
        let kh_Box=document.createElement(newboxname)
        kh_Box.classList.add(classname)
        kh_Box.innerHTML = content;
        OutBoxname.appendChild(kh_Box)
    }
    window.TabBar = TabBar;
    window.$create = CreatBox;
}(window))
